package com.example.hotelapp.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotelapp.Adapter.HomeAdapter;
import com.example.hotelapp.Base.BaseActivity;
import com.example.hotelapp.Bean.HomeIndexBean;
import com.example.hotelapp.R;
import com.example.hotelapp.Utils.AppNetConfig;
import com.example.hotelapp.Utils.DYLoadingView;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static com.example.hotelapp.Utils.MyCommonUtils.ToastShow;
import static com.example.utils.CommonUtils.OKPostValue;
import static com.example.utils.CommonUtils.setSystemBarColor;

/**
 * 查询酒店
 */
public class SelectActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ld)
    DYLoadingView ld;
    private Activity activity;
    private HomeAdapter homeAdapter;

    /**
     * 加载布局
     */
    @Override
    protected int setLayoutId() {
        return R.layout.activity_select;
    }


    /**
     * 初始化
     */
    @Override
    protected void init() {
        activity = this;
        ld.start();
        ld.setVisibility(View.VISIBLE);
        setSystemBarColor(activity, R.color.white);
        String hotelAddress = getIntent().getStringExtra("address");
        String levels = getIntent().getStringExtra("level");
        activity = this;
        String xingji = "星级";
        if (levels != null){
            xingji = levels;
        }
        assert hotelAddress != null;
        OKPostValue("hotelAddress", hotelAddress, "hotelLevel", xingji, AppNetConfig.selectHotel, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ld.stop();
                        ld.setVisibility(View.GONE);
                        ToastShow(activity,"服务器出错，请稍后再试");
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //获取返回的json数据并解析
                String json = response.body().string();
                System.out.println(json);
                HomeIndexBean databean = new Gson().fromJson(json, HomeIndexBean.class);
                int code = databean.getCode();
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ld.stop();
                        ld.setVisibility(View.GONE);
                        if (code == 1) {
                            //请求成功
                            recyclerView.setLayoutManager(new GridLayoutManager(activity, 1));
                            homeAdapter = new HomeAdapter(activity, databean.getData());
                            recyclerView.setAdapter(homeAdapter);
                        } else {
                            ToastShow(activity,"数据加载失败");
                            recyclerView.setAdapter(null);
                        }
                    }
                });
            }
        });
    }

    /**
     * 获取传递过来的参数
     */
    public static void start(Context context, String address, String level) {
        Intent intent = new Intent(context, SelectActivity.class);
        intent.putExtra("address", address);
        intent.putExtra("level", level);
        context.startActivity(intent);
    }

}
